#include <vtkAxis.h>
#include <vtkBrush.h>
#include <vtkChartHistogram2D.h>
#include <vtkColor.h>
#include <vtkColorLegend.h>
#include <vtkColorTransferFunction.h>
#include <vtkContextScene.h>
#include <vtkContextView.h>
#include <vtkDoubleArray.h>
#include <vtkImageData.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTable.h>
#include <vtkTextProperty.h>
#include <vtkVector.h>

//----------------------------------------------------------------------------
int main(int, char*[])
{
    // Define colors
    vtkNew<vtkNamedColors> colors;
//    vtkColor3d backgroundColor = colors->GetColor3d("SlateGray");
//    vtkColor3d titleColor = colors->GetColor3d("Orange");
//    vtkColor3d axisTitleColor = colors->GetColor3d("Orange");
//    vtkColor3d axisLabelColor = colors->GetColor3d("Beige");
//    vtkColor4ub legendBackgroundColor = colors->GetColor4ub("Tomato");

    // Set up a 2D scene, add an XY chart to it
    int size = 400;
    vtkNew<vtkContextView> view;
    view->GetRenderWindow()->SetSize(512, 512);
    view->GetRenderWindow()->SetWindowName("Histogram2D");

//    view->GetRenderer()->SetBackground(backgroundColor.GetData());

    // Define a chart
    vtkNew<vtkChartHistogram2D> chart;

    // Chart Title
    chart->SetTitle("2D Histogram");
    chart->GetTitleProperties()->SetFontSize(36);
//    chart->GetTitleProperties()->SetColor(titleColor.GetData());

    // Chart Axes
    chart->GetAxis(0)->GetTitleProperties()->SetFontSize(24);
//    chart->GetAxis(0)->GetTitleProperties()->SetColor(axisTitleColor.GetData());
//    chart->GetAxis(0)->GetLabelProperties()->SetColor(axisLabelColor.GetData());
    chart->GetAxis(0)->GetLabelProperties()->SetFontSize(18);

    chart->GetAxis(1)->GetTitleProperties()->SetFontSize(24);
    chart->GetAxis(1)->GetTitleProperties()->SetColor(
            colors->GetColor3d("orange").GetData());
    chart->GetAxis(1)->GetLabelProperties()->SetColor(
            colors->GetColor3d("beige").GetData());
    chart->GetAxis(1)->GetLabelProperties()->SetFontSize(18);

    // Chart Legend
    dynamic_cast<vtkColorLegend*>(chart->GetLegend())->DrawBorderOn();
//    chart->GetLegend()->GetBrush()->SetColor(legendBackgroundColor);

    // Add the chart to the view
    view->GetScene()->AddItem(chart);

    vtkNew<vtkImageData> data;
//    data->SetExtent(0, size - 1, 0, size - 1, 0, 0);
//    data->AllocateScalars(VTK_DOUBLE, 1);
//
//    data->SetOrigin(100.0, -100.0, 0.0);
//    data->SetSpacing(2.0, 1.0, 1.0);
//
//    double* dPtr = static_cast<double*>(data->GetScalarPointer(0, 0, 0));
//    for (int i = 0; i < size; ++i)
//    {
//        for (int j = 0; j < size; ++j)
//        {
//            dPtr[i * size + j] =
//                    std::sin(vtkMath::RadiansFromDegrees(double(2 * i))) *
//                    std::cos(vtkMath::RadiansFromDegrees(double(j)));
//
//            std::cout << dPtr[i * size + j] << " ";
//        }
//    }

    int Row = 10;
    int Col = 1000;

//    std::vector<std::vector<double>> list<Row>;
//
//    for (int i = 0; i < Row; i++) {
//        list[i].resize(Col);
//    }
//    for(int i = 0; i < 10; i++)
//    {
//        for(int j = 0; j < 1000; j++)
//        {
//
//        }
//    }

    // Create an image
    data->SetExtent(0, 10 - 1, 0, 200 - 1, 0, 0);
    data->AllocateScalars(VTK_DOUBLE,1);


//    data->SetDimensions(10,10,1);

//    data->SetOrigin(0.0, 100.0, 0.0);
//    data->SetSpacing(1.0, 0.1, 0);
//    data->SetDirectionMatrix()

    int dims[3];
    data->GetDimensions(dims);

//    double * pixel = static_cast<double*>(data->GetScalarPointer(i,0,0));


    for(int i = 0; i < dims[0]; i++)
    {
//        double * pixel = static_cast<double*>(data->GetScalarPointer(i,0,0));

        for(int j = 0; j < dims[1]; j++)
        {
            double * pixel = static_cast<double*>(data->GetScalarPointer(i,j,0));
            pixel[0] = vtkMath::Random(0, 1);
        }
    }

    chart->SetInputData(data);


    vtkNew<vtkColorTransferFunction> transferFunction;
    transferFunction->AddHSVSegment(0.0, 0.0, 1.0, 1.0, 0.3333, 0.3333, 1.0, 1.0);
    transferFunction->AddHSVSegment(0.3333, 0.3333, 1.0, 1.0, 0.6666, 0.6666, 1.0,
                                    1.0);
    transferFunction->AddHSVSegment(0.6666, 0.6666, 1.0, 1.0, 1.0, 0.2, 1.0, 0.3);
    transferFunction->Build();
    chart->SetTransferFunction(transferFunction);

    view->GetRenderWindow()->Render();
    view->GetInteractor()->Start();

    return EXIT_SUCCESS;
}